package com.pearadmin.modules.blog.service.impl;

import com.pearadmin.modules.blog.domain.Article;
import com.pearadmin.modules.blog.domain.Label;
import com.pearadmin.modules.blog.domain.LabelArticle;
import com.pearadmin.modules.blog.mapper.ArticleMapper;
import com.pearadmin.modules.blog.mapper.LabelArticleMapper;
import com.pearadmin.modules.blog.mapper.LabelMapper;
import com.pearadmin.modules.blog.service.IArticleService;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pearadmin.common.web.domain.request.PageDomain;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service("articleService")
public class ArticleServiceImpl implements IArticleService {



    @Resource
    ArticleMapper articleMapper;

    @Resource
    LabelMapper labelMapper;

    @Resource
    LabelArticleMapper labelArticleMapper;

    @Override
    public List<Article> selectList(Article param) {
        return articleMapper.selectList(param);
    }

    @Override
    public PageInfo<Article> page(Article param, PageDomain pageDomain) {
        PageHelper.startPage(pageDomain.getPage(),pageDomain.getLimit());
        List<Article> articles = articleMapper.selectList(param);
        return new PageInfo<>(articles);
    }

    @Override
    @Transactional //添加事务回滚
    public Integer insert(Article article) {

        //1、查询是否存在 标签
        // 不存在则添加，存在获取 id
        // 插入关联表
        article.setCilckCount(0);
        article.setCreateTime(LocalDateTime.now());
        article.setCommentNumber(0);
        article.setRevisionTime(LocalDateTime.now());
        int resultRot = articleMapper.insert(article);
        if(article.getLabels() != null && article.getLabels().length() != 0){
            String[] lables = article.getLabels().split(",");
            for(String lab : lables){
                // 不能直接新增，不存在则新增
                Label label = labelMapper.selectByName(lab);
                if(label == null){
                    label = new Label();
                    label.setLableName(lab);
                    labelMapper.insert(label);
                }

                LabelArticle labelArticle = new LabelArticle();
                labelArticle.setArticleId(article.getId());
                labelArticle.setLabelId(label.getId());
                labelArticleMapper.insert(labelArticle);
            }
        }


        return 0;
    }

    @Override
    public Article selectById(Integer id) {
        return articleMapper.selectById(id);
    }

    @Override
    public Integer updateById(Article article) {
        return articleMapper.updateById(article);
    }

    @Override
    public Integer deleteById(Integer id) {
        return articleMapper.deleteById(id);
    }

    @Override
    public Integer deleteByIds(Object[] ids) {
        return articleMapper.deleteByIds(ids);
    }

    @Override
    public List<Article> findTop10() {
        return articleMapper.findTop10();
    }

}
